//JavaScript Document

var db;
var dbCreated = false;
var day, year, month;
var isSave = true;
var enable_click = true;
var dateArray = [];

document.addEventListener("deviceready", onDeviceReady, true);
function onDeviceReady() {
	$('#delete_event').hide();
	$(".sukien_btn").click(function(){
		if (enable_click){
			enable_click = false;
			var date = new Date();
			day = date.getDate();
			month = date.getMonth() + 1;
			year = date.getFullYear();
			$('#ngayhomnay').html("Hôm nay là ngày " + day + " tháng " + month + " năm " + year);

			db = window.openDatabase("LichVanSu", "1.0", "Lịch Vạn Sự", 200000);
			db.transaction(populateDB, transaction_error, populateDB_success);
			db.transaction(getAllEvent, getAllEvent_error, getAllEvent_success);
			db.transaction(getEventToDay, getEventToDay_error, getEventToDay_success);
            
			$('#sukien_datebox').bind('datebox', function(e, passed){
				if (passed.method === "set") {
					$.mobile.changePage('#page_edit_event', {
						role : 'dialog'	
					});

					var splited = passed.value.split('/');
					day = splited[0];
					month = splited[1];
					year = splited[2];
					$('#ngay_sukien').html("Sự kiện ngày " + passed.value);
					if (enable_click){
						db.transaction(getEventByDate, selected_error);
						enable_click = false;
					}
				}
			});

			//Creat DB

			function populateDB (tx){
				//tx.executeSql('DROP TABLE IF EXISTS event');
				var sql = 
					"CREATE TABLE IF NOT EXISTS event ( "+
					"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
					"name TEXT, " +
					"content TEXT, " +
					"day INTEGER, " +
					"month INTEGER, " +
					"year INTEGER )" ;
				tx.executeSql(sql);
			}

			function transaction_error(tx, error) {
				//alert("Database Error: " + error);
				enable_click = true;
			}

			function selected_error (tx, error){
				//alert ("selected_error" + error);
				$('#delete_event').hide();
				$('#save_event').val("Lưu");
				isSave = true;
				$('#ten_sukien').val("");
				$('#noidung_sukien').val("");
				enable_click = true;
			}
			function populateDB_success() {
				enable_click = true;
				dbCreated = true;
			}
			// lấy su kien theo ngay

			function getEventByDate (tx){
				var sql = "SELECT * FROM event WHERE " +
				"day = " + day + " AND " +
				"month = " + month + " AND " +
				"year = " + year ;
				//alert(sql);
				tx.executeSql(sql, [], getEvent_success);	
			}

			function getEvent_success (tx, results){
				enable_click = true;
				var employee = results.rows.item(0);
				isSave = false;
				//alert (employee.name + " / " + employee.content);
				$('#delete_event').show();
				$('#ten_sukien').val(employee.name);
				$('#noidung_sukien').val(employee.content);
			}
			// Lấy sự kiện ngày hôm nay
			
			function getEventToDay (tx){
				var date = new Date();
				day = date.getDate();
				month = date.getMonth() + 1;
				year = date.getFullYear();
				var sql = "SELECT * FROM event WHERE " +
				"day = " + day + " AND " +
				"month = " + month + " AND " +
				"year = " + year ;
				//alert(sql);
				tx.executeSql(sql, [], getEventToDay_success);	
			}
			function getEventToDay_error (tx, error){
				//alert ("to day error");
				$('#homnay').html("Ngày hôm nay không có sự kiện");
			}

			function getEventToDay_success (tx, results){
				var employee = results.rows.item(0);
				//alert (employee.name + " / " + employee.content);
				$('#homnay').html("Sự kiện: " + employee.name + "<br />" + employee.content);
			} 

			// ThÃªm sá»± kiá»‡n

			function insertEvent (tx){
				var sql = "INSERT INTO event (name, content, day, month, year) VALUES " +
				"('"+$('#ten_sukien').val()+"', '"+$('#noidung_sukien').val()+"', "+day+", "+month+", "+year+")"
				//alert(sql);
				tx.executeSql(sql);
			}

			function insertEvent_success (tx){
				$('#ten_sukien').val(" ");
				$('#noidung_sukien').val(" ");
				//alert (" Insert Success");
				enable_click = true;
			}

			// Sá»­a sá»± kiá»‡n 

			function updateEvent(tx){
				var sql = "UPDATE event SET name = '" + $('#ten_sukien').val() + "'," +
				"content = '" + $('#noidung_sukien').val() + "'" + "WHERE " +
				"day = " + day + " AND " +
				"month = " + month + " AND " +
				"year = " + year ;
				tx.executeSql(sql);
			}

			function updateEvent_success (){
				$('#ten_sukien').val(" ");
				$('#noidung_sukien').val(" ");
				//alert ("update success");
				enable_click = true;
			}

			// Lay tat ca su kien

			function getAllEvent (tx){
				var sql = "SELECT * FROM event";
				tx.executeSql(sql, [], getAllEvent_success);
			}

			function getAllEvent_error (tx, error){

			}

			function getAllEvent_success (tx, results){
				var len = results.rows.length;
				console.log(len);
				dateArray = [];
				for (var i=0; i<len; i++) {
					var employee = results.rows.item(i);
					var temp_day = employee.day;
					var temp_month = employee.month;
					if (temp_day < 10){
						temp_day = "0" + temp_day;	
					}
					if (temp_month < 10){
						temp_month = "0" + temp_month;	
					}
					var temp_date = employee.year + "-" + temp_month + "-" + temp_day;
					//alert (temp_date);
					dateArray.push(temp_date);
				}
				for (var i = 0; i<dateArray.length; i++){
					//alert (dateArray[i]);	
				}
				$('#sukien_datebox').datebox("option", {
					highDatesAlt: dateArray
				});
			}
			//Xóa sự kiện
			function deleteEvent (tx){
				var sql = "DELETE FROM event WHERE " +
				"day = " + day + " AND " +
				"month = " + month + " AND " +
				"year = " + year ;
				tx.executeSql(sql);
			}

			function deleteEvent_error (tx, error){
			}

			function deleteEvent_success (tx){
				$('#ten_sukien').val(" ");
				$('#noidung_sukien').val(" ");
				//alert (" Insert Success");
				enable_click = true;	
			}

			// bặt sự kiện save
			$('#save_event').click(function(){
				if (enable_click){
					if (isSave){
						db.transaction(insertEvent, transaction_error, insertEvent_success);
					}else {
						db.transaction(updateEvent, transaction_error, updateEvent_success);
					}
					db.transaction(getAllEvent, getAllEvent_error, getAllEvent_success);
					db.transaction(getEventToDay, getEventToDay_error, getEventToDay_success);
					$.mobile.changePage('#sukien');
					enable_click = false;
				}
			});

			$('#delete_event').click(function(){
				if (enable_click){
					db.transaction(deleteEvent, deleteEvent_error, deleteEvent_success);
					db.transaction(getAllEvent, getAllEvent_error, getAllEvent_success);
					db.transaction(getEventToDay, getEventToDay_error, getEventToDay_success);
					$.mobile.changePage('#sukien');
					enable_click = false;
				}
			});
		}
	});
}